package com.thrc.borrow.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.Menu;
import com.thrc.borrow.service.MenuService;
import com.thrc.borrow.utils.Container;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.MenuVo;
import com.thrc.borrow.vo.UserVo;

@Controller
@RequestMapping("/system")
public class SystemController {

	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/manager")
	public String goMenuJsp(HttpServletRequest request) {
		request.setAttribute("userPhone", request.getSession().getAttribute("userPhone"));
		return "manager";
	}

	@RequestMapping(value = "/tologin")
	public String tologin() {

		return "login";
	}

	@RequestMapping(value = "/register")
	public String register() {

		return "register";
	}

	/**
	 * 给前台的方法，判断用户是否登录，在登录拦截(loginInspecterceptor.java)里面要放行
	 * 
	 * @return
	 */
	@RequestMapping(value = "/checkLogin")
	public void checkLogin(HttpServletRequest request, HttpServletResponse response) {

		if (request.getSession().getAttribute("user") != null) {
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE, "用户已登录");
			return;
		}

		ResponseUtil.print(response, ReturnCode.FAIL_CODE, "用户未登录");
	}

	@RequestMapping(value = "/loadfile")
	public String loadfile() {

		return "fileUpload";
	}

	@RequestMapping(value = "/selectAllMenu")
	public void selectAllMenu(HttpServletRequest request, HttpServletResponse response) {
		Menu t = new Menu();
		List<MenuVo> menuList = menuService.selectAll(t);
		ResponseUtil.print(response, menuList);
	}

	@RequestMapping(value = "/getEnableJson")
	public void getEnableJson(HttpServletResponse response) {

		JSONArray retJson = new JSONArray();

		JSONObject disableJson = new JSONObject();
		disableJson.put(ReturnCode.RETURN_CODE, Container.USER.STATIS_DISABLE);
		disableJson.put(ReturnCode.RETURN_DATA, "无效的");

		JSONObject enableJson = new JSONObject();
		enableJson.put(ReturnCode.RETURN_CODE, Container.USER.STATIS_ENABLE);
		enableJson.put(ReturnCode.RETURN_DATA, "有效的");

		retJson.add(disableJson);
		retJson.add(enableJson);

		ResponseUtil.print(response, retJson);

	}

	@RequestMapping("/getStatus")
	public void getStatus(HttpServletResponse response) {
		JSONArray array = new JSONArray();
		JSONObject object1 = new JSONObject();
		object1.put("id", "1");
		object1.put("text", "待审核");
		array.add(object1);
		JSONObject object2 = new JSONObject();
		object2.put("id", "2");
		object2.put("text", "待还款");
		array.add(object2);
		JSONObject object3 = new JSONObject();
		object3.put("id", "3");
		object3.put("text", "已还清");
		array.add(object3);
		/*
		 * JSONObject object4 = new JSONObject(); object4.put("id", "4");
		 * object4.put("text", "已抢单"); array.add(object4);
		 */

		ResponseUtil.print(response, array);
	}
}
